package com.ruoyi.project.hcy.product.service.impl;

import com.ruoyi.common.exception.hcy.product.IllegalCountException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.hcy.product.domain.favorites.ProFavorites;
import com.ruoyi.project.hcy.product.domain.product.Product;
import com.ruoyi.project.hcy.product.domain.product.ProductNameVo;
import com.ruoyi.project.hcy.product.domain.product.ProductVo;
import com.ruoyi.project.hcy.product.domain.specification.ProSpecification;
import com.ruoyi.project.hcy.product.mapper.ProFavoritesMapper;
import com.ruoyi.project.hcy.product.mapper.ProductMapper;
import com.ruoyi.project.hcy.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ProductServiceImpl
 * @Author SAI
 * @Date 2020/9/18 16:05
 * @Description ProductServiceImpl
 * @Version 1.0
 */
@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProFavoritesMapper proFavoritesMapper;

    @Override
    public List<ProductVo> selectProductList(Product product) {
        return productMapper.selectProductList(product);
    }

    @Override
    public String checkProductNameUnique(Product product) {
        Long productId = StringUtils.isNull(product.getProductId()) ? -1L : product.getProductId();
        Product info = productMapper.checkProductNameUnique(product.getProductName());
        if (StringUtils.isNotNull(info) && info.getProductId().longValue() != productId.longValue()) {
            return "1";
        }
        return "0";
    }

    @Override
    public int insertProduct(Product product) {
        return productMapper.insertProduct(product);
    }

    @Override
    public Product selectProductById(Long productId) {
        return  productMapper.selectProductById(productId);
    }

    @Override
    public int updateProduct(Product product) {
        return productMapper.updateProduct(product);
    }

    @Override
    public int deleteProductByIds(String ids) {
        Long[] longs = Convert.toLongArray(ids);
        return productMapper.deleteProductByIds(longs);
    }

    @Override
    public List<ProductVo> selectFavoritesProductList(Product product) {
        System.out.println("开始执行");
        List<ProFavorites>  favlist = proFavoritesMapper.selectFavoritesList();
        List<Long> list = new ArrayList<>();
        for (ProFavorites proFavorites : favlist) {
            list.add(proFavorites.getProductId());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("product",product);
        map.put("ids",list);
        System.out.println("查询集合");
        return productMapper.selectFavoritesProductList(map);
    }

    @Override
    public ProductNameVo selectProductNameById(Long productId) {
        return productMapper.selectProductNameById(productId);
    }

    @Override
    public List<ProductVo> selectShowList(Product product) {
        return productMapper.selectShowList(product);
    }

    @Override
    public int changeStatus(String ids) {
        Long[] longs = Convert.toLongArray(ids);
        return productMapper.changeStatus(longs);
    }
}
